package com.huike.review.controller;

import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@Slf4j
@RestController
@RequestMapping("/review")
public class ReviewController extends BaseController {
    @Autowired
    private ReviewService reviewService;

    /**
     * 添加用户
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        Integer count = reviewService.save(name, age, sex);
        return AjaxResult.success("成功插入:"+count+"条数据");
    }
    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody MybatisReviewVO mybatisReviewVO){
       Integer count = reviewService.add(mybatisReviewVO);

        return AjaxResult.success("成功插入:"+count+"条数据");
    }

    /**
     * 更新数据
     * @param mybatisReviewVO
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO){
         reviewService.update(mybatisReviewVO);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Long id){
        Long count = reviewService.delete(id);
        return AjaxResult.success("成功删除:"+count+"条数据");
    }
    /**
     * 根据id来查询数据
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult select(@RequestParam Long id){
       Review review = reviewService.select(id);

       return AjaxResult.success(review);
    }
    @GetMapping("/getDataByPage")
    public TableDataInfo page(){
      /* List<Review> reviewList= reviewService.page(pageNum,pageSize);
       Long total = reviewService.total(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(reviewList);
        tableDataInfo.setTotal(total);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");*/
//        return AjaxResult.success(tableDataInfo);

        startPage();
        List<Review> reviewList= reviewService.page();

        return getDataTablePage(reviewList);
    }
}
